package utility.statistik;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;

/**
 * Denne klasse er udviklet på baggrund af, at man let skal kunne generere en
 * grafisk afbildning af nogle data. Det vil oftest være i statistik-sammenhænge
 * at denne vil blive anvendt.
 *
 * @author Jeppe
 */
public class StatistikModul extends javax.swing.JPanel {

    private JLabel[] yAxisLabels;
    private String unit;
    private ArrayList<StatistikData> statistikDatas;
    private final Font textFont;

    /**
     * Unit is default percent. Can be changed with setUnit(String unit).
     *
     * @param diagramTitel
     */
    public StatistikModul(String diagramTitel) {
        initComponents();
        statistikDatas = new ArrayList<>();
        diagramTitelLabel.setText(diagramTitel);
        yAxisLabels = new JLabel[]{yMinLabel, del1Label, del2Label, del3Label, del4Label, del5Label, del6Label, del7Label, del8Label, del9Label, yMaxLabel};
        unit = "";
        textFont = new Font("Century Gothic", 0, 12);
    }

    public void addStatistikData(String title, int value, Color color) {
        if (color == null) {
            statistikDatas.add(new StatistikData(title, value));
        } else {
            statistikDatas.add(new StatistikData(title, value, color));
        }
        
        dataPanel.removeAll();

        int yCoordinateColor = 18;
        int yCoordinateTitle = 20;
        final int xColor = 10;
        final int xTitle = 40;
        for (int i = 0; i < statistikDatas.size(); i++) {
            JLabel colorLabel = new JLabel();
            colorLabel.setName(statistikDatas.get(i).getTitle() + "Label");
            colorLabel.setBackground(statistikDatas.get(i).getColor());
            colorLabel.setBorder(new LineBorder(Color.WHITE, 2));
            colorLabel.setOpaque(true);
            dataPanel.add(colorLabel);
            colorLabel.setBounds(xColor, yCoordinateColor, 20, 20);
            yCoordinateColor = yCoordinateColor + 27;

            JLabel titleLabel = new JLabel(statistikDatas.get(i).getTitle());
            titleLabel.setFont(textFont);
            dataPanel.add(titleLabel);
            titleLabel.setBounds(xTitle, yCoordinateTitle, 90, 14);
            yCoordinateTitle = yCoordinateTitle + 27;
        }
        dataPanel.setSize(dataPanel.getWidth(), yCoordinateTitle + 10);
        
    }

    /**
     * Sets the Unit on the y-axis
     *
     * @param unit Should only be String.lenght = 1
     */
    public void setUnit(String unit) {
        if (unit == null) {
            this.unit = "";
        } else {
            this.unit = unit;
        }
    }

    /**
     * Sets all labels on the y-axis
     *
     * @param startY An integer. Must be lower than endY
     * @param endY An integer. Must be higher than startY
     */
    private void setStartAndEndYValue(int startY, int endY) {
        int delta = endY - startY;
        double interval = (double) delta / 10.0;
        double index = startY;
        for (JLabel jLabel : yAxisLabels) {
            jLabel.setText(Math.round(index) + unit);
            index = index + interval;
        }
    }

    public void generérStatistik() {

        int yCoordinate = jSeparator11.getY();
        double pixelPercent = (double) 350 / (double) 100;

        int totalValue = 0;
        for (int i = 0; i < statistikDatas.size(); i++) {
            totalValue = totalValue + statistikDatas.get(i).getValue();
        }
        if(totalValue == 0){
            totalValue = 100;
            
        }
        setStartAndEndYValue(0, totalValue);
        if (totalValue != 0) {
            for (int i = statistikDatas.size()-1; i >=0 ; i--) {
                double calcPercent = ((double) statistikDatas.get(i).getValue() / (double) totalValue) * 100.00;
                double heightdouble = pixelPercent * calcPercent;
                int height = (int) Math.round(heightdouble);
                JButton jButton = new JButton(statistikDatas.get(i).toString());
                jButton.setFont(new Font("Century Gothic", 0, 16));
                diagramPanel.add(jButton, 0);
                jButton.setBounds(65, yCoordinate - height, 450, height);
//                jButton.setContentAreaFilled(false);
                jButton.setBackground(statistikDatas.get(i).getColor());
                jButton.setVisible(true);
//                jButton.setOpaque(true);
                yCoordinate = yCoordinate - height;
            }
        }
        diagramPanel.repaint();
        diagramPanel.revalidate();

        revalidate();
        repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        diagramTitelLabel = new javax.swing.JLabel();
        diagramPanel = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jSeparator4 = new javax.swing.JSeparator();
        jSeparator5 = new javax.swing.JSeparator();
        jSeparator6 = new javax.swing.JSeparator();
        jSeparator7 = new javax.swing.JSeparator();
        jSeparator8 = new javax.swing.JSeparator();
        jSeparator9 = new javax.swing.JSeparator();
        jSeparator10 = new javax.swing.JSeparator();
        jSeparator11 = new javax.swing.JSeparator();
        yMaxLabel = new javax.swing.JLabel();
        del9Label = new javax.swing.JLabel();
        del8Label = new javax.swing.JLabel();
        del7Label = new javax.swing.JLabel();
        del6Label = new javax.swing.JLabel();
        del5Label = new javax.swing.JLabel();
        del4Label = new javax.swing.JLabel();
        del3Label = new javax.swing.JLabel();
        del2Label = new javax.swing.JLabel();
        del1Label = new javax.swing.JLabel();
        yMinLabel = new javax.swing.JLabel();
        dataPanel = new javax.swing.JPanel();
        serie1ColorLabel = new javax.swing.JLabel();
        serie2ColorLabel = new javax.swing.JLabel();
        serie3ColorLabel = new javax.swing.JLabel();
        serie1Label = new javax.swing.JLabel();
        serie2Label = new javax.swing.JLabel();
        serie3Label = new javax.swing.JLabel();

        setOpaque(false);
        setLayout(null);

        diagramTitelLabel.setFont(new java.awt.Font("Century Gothic", 1, 22)); // NOI18N
        diagramTitelLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        diagramTitelLabel.setText("DiagramTitel");
        add(diagramTitelLabel);
        diagramTitelLabel.setBounds(10, 20, 530, 28);

        diagramPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(204, 204, 204), null));
        diagramPanel.setOpaque(false);
        diagramPanel.setLayout(null);

        jSeparator1.setBackground(new java.awt.Color(0, 0, 0));
        jSeparator1.setForeground(new java.awt.Color(255, 255, 255));
        jSeparator1.setOpaque(true);
        diagramPanel.add(jSeparator1);
        jSeparator1.setBounds(55, 20, 470, 2);

        jSeparator2.setBackground(new java.awt.Color(0, 0, 0));
        jSeparator2.setForeground(new java.awt.Color(255, 255, 255));
        jSeparator2.setOpaque(true);
        diagramPanel.add(jSeparator2);
        jSeparator2.setBounds(55, 55, 470, 2);

        jSeparator3.setBackground(new java.awt.Color(0, 0, 0));
        jSeparator3.setForeground(new java.awt.Color(255, 255, 255));
        jSeparator3.setOpaque(true);
        diagramPanel.add(jSeparator3);
        jSeparator3.setBounds(55, 90, 470, 2);

        jSeparator4.setBackground(new java.awt.Color(0, 0, 0));
        jSeparator4.setForeground(new java.awt.Color(255, 255, 255));
        jSeparator4.setOpaque(true);
        diagramPanel.add(jSeparator4);
        jSeparator4.setBounds(55, 125, 470, 2);

        jSeparator5.setBackground(new java.awt.Color(0, 0, 0));
        jSeparator5.setForeground(new java.awt.Color(255, 255, 255));
        jSeparator5.setOpaque(true);
        diagramPanel.add(jSeparator5);
        jSeparator5.setBounds(55, 160, 470, 2);

        jSeparator6.setBackground(new java.awt.Color(0, 0, 0));
        jSeparator6.setForeground(new java.awt.Color(255, 255, 255));
        jSeparator6.setOpaque(true);
        diagramPanel.add(jSeparator6);
        jSeparator6.setBounds(55, 195, 470, 2);

        jSeparator7.setBackground(new java.awt.Color(0, 0, 0));
        jSeparator7.setForeground(new java.awt.Color(255, 255, 255));
        jSeparator7.setOpaque(true);
        diagramPanel.add(jSeparator7);
        jSeparator7.setBounds(55, 230, 470, 2);

        jSeparator8.setBackground(new java.awt.Color(0, 0, 0));
        jSeparator8.setForeground(new java.awt.Color(255, 255, 255));
        jSeparator8.setOpaque(true);
        diagramPanel.add(jSeparator8);
        jSeparator8.setBounds(55, 265, 470, 2);

        jSeparator9.setBackground(new java.awt.Color(0, 0, 0));
        jSeparator9.setForeground(new java.awt.Color(255, 255, 255));
        jSeparator9.setOpaque(true);
        diagramPanel.add(jSeparator9);
        jSeparator9.setBounds(55, 300, 470, 2);

        jSeparator10.setBackground(new java.awt.Color(0, 0, 0));
        jSeparator10.setForeground(new java.awt.Color(255, 255, 255));
        jSeparator10.setOpaque(true);
        diagramPanel.add(jSeparator10);
        jSeparator10.setBounds(55, 335, 470, 2);

        jSeparator11.setBackground(new java.awt.Color(0, 0, 0));
        jSeparator11.setForeground(new java.awt.Color(0, 0, 0));
        jSeparator11.setOpaque(true);
        diagramPanel.add(jSeparator11);
        jSeparator11.setBounds(55, 370, 470, 3);

        yMaxLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        yMaxLabel.setText("yMax");
        diagramPanel.add(yMaxLabel);
        yMaxLabel.setBounds(10, 15, 40, 15);

        del9Label.setText("9. del");
        diagramPanel.add(del9Label);
        del9Label.setBounds(10, 50, 40, 14);

        del8Label.setText("8. del");
        diagramPanel.add(del8Label);
        del8Label.setBounds(10, 83, 40, 14);

        del7Label.setText("7. del");
        diagramPanel.add(del7Label);
        del7Label.setBounds(10, 120, 40, 14);

        del6Label.setText("6. del");
        diagramPanel.add(del6Label);
        del6Label.setBounds(10, 154, 40, 14);

        del5Label.setText("5. del");
        diagramPanel.add(del5Label);
        del5Label.setBounds(10, 190, 40, 14);

        del4Label.setText("4. del");
        diagramPanel.add(del4Label);
        del4Label.setBounds(10, 224, 40, 14);

        del3Label.setText("3. del");
        diagramPanel.add(del3Label);
        del3Label.setBounds(10, 260, 40, 14);

        del2Label.setText("2. del");
        diagramPanel.add(del2Label);
        del2Label.setBounds(10, 294, 40, 14);

        del1Label.setText("1. del");
        diagramPanel.add(del1Label);
        del1Label.setBounds(10, 330, 40, 14);

        yMinLabel.setText("0. del");
        diagramPanel.add(yMinLabel);
        yMinLabel.setBounds(10, 366, 40, 14);

        add(diagramPanel);
        diagramPanel.setBounds(3, 60, 540, 400);

        dataPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        dataPanel.setOpaque(false);
        dataPanel.setLayout(null);

        serie1ColorLabel.setBackground(new java.awt.Color(255, 51, 255));
        serie1ColorLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255), 2));
        serie1ColorLabel.setOpaque(true);
        dataPanel.add(serie1ColorLabel);
        serie1ColorLabel.setBounds(10, 18, 20, 20);

        serie2ColorLabel.setBackground(new java.awt.Color(0, 204, 0));
        serie2ColorLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255), 2));
        serie2ColorLabel.setOpaque(true);
        dataPanel.add(serie2ColorLabel);
        serie2ColorLabel.setBounds(10, 45, 20, 20);

        serie3ColorLabel.setBackground(new java.awt.Color(0, 51, 255));
        serie3ColorLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255), 2));
        serie3ColorLabel.setOpaque(true);
        dataPanel.add(serie3ColorLabel);
        serie3ColorLabel.setBounds(10, 72, 20, 20);

        serie1Label.setText("Serie 1");
        dataPanel.add(serie1Label);
        serie1Label.setBounds(40, 20, 90, 14);

        serie2Label.setText("Serie 2");
        dataPanel.add(serie2Label);
        serie2Label.setBounds(40, 47, 90, 14);

        serie3Label.setText("Serie 3");
        dataPanel.add(serie3Label);
        serie3Label.setBounds(40, 74, 90, 14);

        add(dataPanel);
        dataPanel.setBounds(550, 60, 150, 110);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel dataPanel;
    private javax.swing.JLabel del1Label;
    private javax.swing.JLabel del2Label;
    private javax.swing.JLabel del3Label;
    private javax.swing.JLabel del4Label;
    private javax.swing.JLabel del5Label;
    private javax.swing.JLabel del6Label;
    private javax.swing.JLabel del7Label;
    private javax.swing.JLabel del8Label;
    private javax.swing.JLabel del9Label;
    private javax.swing.JPanel diagramPanel;
    private javax.swing.JLabel diagramTitelLabel;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator10;
    private javax.swing.JSeparator jSeparator11;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JSeparator jSeparator7;
    private javax.swing.JSeparator jSeparator8;
    private javax.swing.JSeparator jSeparator9;
    private javax.swing.JLabel serie1ColorLabel;
    private javax.swing.JLabel serie1Label;
    private javax.swing.JLabel serie2ColorLabel;
    private javax.swing.JLabel serie2Label;
    private javax.swing.JLabel serie3ColorLabel;
    private javax.swing.JLabel serie3Label;
    private javax.swing.JLabel yMaxLabel;
    private javax.swing.JLabel yMinLabel;
    // End of variables declaration//GEN-END:variables
}
